﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Threading;
using System.Collections.Generic;
using System.EnterpriseServices;
public partial class Admin_AdminRole : basePage
{
    rolePower rp = new rolePower();
    roleBll rBll = new roleBll();
    role roleModel = new role();
    PowerBll pBll = new PowerBll();
    power powerModel = new power();
    rolePowerBll rpBll = new rolePowerBll();
    StringBuilder sb = new StringBuilder();
    userRoleBll urBll = new userRoleBll();
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {
                string action = Request.QueryString["action"].ToString();
                string id = "";
                int roleId =Convert.ToInt32(Request["roleId"].ToString());
                this.roleId_hid.Value = roleId.ToString();               
                switch (action.Trim())
                {
                    case "add":                       
                        addRole();
                        break;
                    case "del":                       
                        del(roleId);
                        break;
                    case "edit":  
                         this.roleId_hid.Value = roleId.ToString();
                         EditRole(roleId); 
                        break;
                    case "update":                       
                        updateRole(roleId);                      
                        break;
                    case "delTb":
                        delTb(roleId);
                        break;
                    default:
                        break;
                }
            }
            bindRole();            
        }

    }

    private void delTb(int roleId)
    {
        if (urBll.checkUserByRoleId(roleId) && rpBll.GetModelByRoleId(roleId).RolePowerId >0)
        {
            lock (objSync)
            {
                urBll.Delete(roleId, 2);
                rpBll.delByRoleId(roleId);
            }
        }
        else if (urBll.checkUserByRoleId(roleId))
        {
            urBll.Delete(roleId, 2);
        }
        else if (rpBll.GetModelByRoleId(roleId).RolePowerId>0)
        {
            rpBll.delByRoleId(roleId);
        }
        else
        { }
    }
    #region 显示角色---郑同飞
    /// <summary>
    /// 显示角色---郑同飞
    /// </summary>   
    private void bindRole() 
    {
        this.Repeater1.DataSource = rBll.GetModel();
        this.Repeater1.DataBind();
    } 
    #endregion

    #region  根据角色id得到该角色的权限---郑同飞
    /// <summary>
    /// 根据角色id得到该角色的权限---郑同飞
    /// </summary>
    /// <param name="roleId"></param>
    /// <returns></returns>
    public string getPowerBuyRoleId(int roleId) 
    { 
        sb.Remove(0, sb.Length);
        rp = rpBll.GetModelByRoleId(roleId);       
        if (rp.PowerId!=""&&rp.PowerId!=null)
        {
            string[] strPower = rp.PowerId.Split('|');
            for (int i = 0; i < strPower.Length; i++)
            {
                powerModel = pBll.GetModel(Convert.ToInt32(strPower[i]));
                sb.Append(powerModel.Powername + ":");
                if (i==8)
                {
                    sb.Append("<br/>");
                }
            }          
        }
        else 
        {
            sb.Append("<font color='red'>没有赋予权限！</font>");
        }
        return sb.ToString();
    }
    #endregion   

    #region 添加角色---郑同飞
    /// <summary>
    /// 添加角色---郑同飞
    /// </summary>
    /// <param name="id"></param>
    public void addRole()
    {
        roleModel.Rolename = Request.Form["roleName"].ToString();
        roleModel.Description = Request.Form["Des"].ToString();
        if (rBll.Insert(roleModel))
        {

        }
        else
        {
            Response.Write("失败!!");
        }
      
    }
    #endregion

    #region 修改角色---郑同飞
    /// <summary>
    /// 修改角色---郑同飞
    /// </summary>
    /// <param name="id"></param>
    /// <param name="roleId"></param>
    public void updateRole(int roleId)
    {
        roleModel.Roleid = roleId;
        roleModel.Rolename = Request.Form["roleName"].ToString();
        roleModel.Description = Request.Form["Des"].ToString();
        if (rBll.Update(roleModel))
        {
        }
        
    }
    #endregion

    #region 删除数据---郑同飞
    /// <summary>
    /// 删除数据---郑同飞
    /// </summary>
    /// <param name="id"></param>
    public void del(int id)
    {
        if (!urBll.checkUserByRoleId(id) && rpBll.GetModelByRoleId(id).RolePowerId==0)
        {
            lock (objSync)
            {
                rpBll.delByRoleId(id);
                rBll.Delete(id);
            }
        }
        else 
        {
            Response.Write("<script>alert('无法删除该角色！')</script>");
        }
    }
    #endregion

    #region 给html赋值---郑同飞
    public void EditRole(int roleId)
    {
        roleModel=rBll.GetModel(roleId);
        this.txt_roleName.Value = roleModel.Rolename;
        this.txt_Description.Value = roleModel.Description;        
    }
    #endregion
}
